#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <pthread.h>

//#define HDX019_NODE "/dev/ctrl"
#define HDX019_NODE "ctrl.dat"
#define MAX_BUFFER_COUNT 128
#define MAX_RETRY_TIMES 10



struct hx019_ir {
	pthread_t thd;
};


char learn_cmd[] = {0x30, 0x20, 0x50, 0x00};

char buff[MAX_BUFFER_COUNT];


unsigned char buf_1[]	={0x30,0x00,0x01,0x00,0xff,0x00,0xFF,0x20,0x88,0xD7};
unsigned char buf_2[] ={0x30,0x00,0x01,0x55,0xaa,0x02,0xfd,0x77,0x88,0x2E};



unsigned char carrier_38K[] ={0x30,0x30,0x60};
unsigned char carrier_56K[] ={0x30,0x40,0x70};
unsigned char carrier_30K[] ={0x30,0x31,0x61};
unsigned char carrier_33K[] ={0x30,0x32,0x62};
unsigned char carrier_42K[] ={0x30,0x33,0x63};
unsigned char carrier_70K[] ={0x30,0x34,0x64};
unsigned char carrier_24K[] ={0x30,0x35,0x65};
unsigned char carrier_80K[] ={0x30,0x36,0x66};



void * ir_routine(void *args)
{
	FILE * fp;
	int i = 0;
	int rv;
	int n;
	if(NULL==(fp=fopen(HDX019_NODE, "w"))){
		fprintf(stderr, "open %s fail: %s\n", HDX019_NODE, strerror(errno));
		return NULL;
	}
	n = sizeof(buf_1) / sizeof(buf_1[0]);
	for(i=0; i<MAX_RETRY_TIMES; i++){
		fprintf(stdout, "send ir for %d times\n", i);
		rv = fwrite(buf_1, 1, n, fp);
		if (rv != n){
			fprintf(stderr, "write fail, expect %d, send %d\n", n, rv);
		}
		sleep(2);
	}
	fclose(fp);
	return NULL;
}

int main(int ac ,char *av)
{
	struct hx019_ir ir;
	int err;
	printf("%s .....\n", __func__);
	err = pthread_create(&ir.thd, NULL, ir_routine, (void*)&ir);
	if(err < 0){
		fprintf(stderr, "pthread_create fail, error=%d\n", err);
	}
	err = pthread_join(ir.thd, NULL);
	if(err < 0){
		fprintf(stderr, "pthread_join fail, error=%d\n", err);
	}

	return 0;
}